import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";

import { createPinia } from "pinia";
import useGlobalStore from "./stores/global.store";
const pinia = createPinia();
const app = createApp(App);

app.use(pinia).use(router).mount("#app");

function initGlobalData() {
    let token = localStorage.getItem("token");
    let userInfo = localStorage.getItem("userInfo");
    const globalStore = useGlobalStore();
    if (token) {
        const { setToken } = globalStore;
        setToken(token);
    }
    if (userInfo) {
        const { setUserInfo } = globalStore;
        setUserInfo(JSON.parse(userInfo));
    }
}

initGlobalData();

window.onerror = (event, source, lineno, colno, error) => {
    console.log(event, source, lineno, colno, error);
};

window.addEventListener("keyup", (event) => {
    // 刷新页面时保存文件信息
    if (event.key === "F5") {
        window.saveFileInfo && window.saveFileInfo();
        window.location.reload();
    }
});

window.preload &&
    window.preload.onCloseWindow(() => {
        // 窗口关闭时保存文件信息
        window.saveFileInfo && window.saveFileInfo();
    });

console.log(import.meta.env);
